unit LogRatiosChartUnit;

interface

uses DiagramFactory, datacontainer, pganalysistypesunit, apgabstractunit, classes,
   xmlintf, pgseriesunit, chartaxisunit, ifmmemoryunit, math, sysutils;

type
   LogRatiosChart = class(DSChart)
   private
      m_Age : Integer;
   public
      function CreateChartAxis(Axis : PgAxisType) : APgChartAxis; override;
      function CreateSeries(AOwner : TComponent; PrimaryChartAxis : APgChartAxis; MetaSeries : APgMetaSeries; XIndex : Integer) : APgSeries; override;
      constructor Create(AOwner : TComponent; datacontainer : TasacsDataContainer; _Ds : Integer; Age : Integer); reintroduce;
      property Age : Integer read m_Age;
   end; // LogRatiosChart


   LogRatiosSeries = class(PgSeries)
   protected
      function GetInternalValue(I : Integer; SeriesVariable : PgSeriesVariable; MetaSeries : ApgMetaSeries) : Double; override;
      function GetShowZeros() : Boolean; override;
   end; // LogRatiosSeries


   LogRatioYearAxis = class(DsYearAxis)
   protected
      function GetNumDimensionElms : Integer; override;
   end; // LogRatioYearAxis


implementation

//////////////////////////////////
// LogRatiosChart


function LogRatiosChart.CreateChartAxis(Axis : PgAxisType) : APgChartAxis;
begin
   case Axis of
      atx:
         Result := LogRatioYearAxis.Create(Self as IChart, Axis);
   else
      Result := PgChartAxis.Create(Self as IChart, Axis);
   end; // case
end; // LogRatiosChart.CreateChartAxis


function LogRatiosChart.CreateSeries(AOwner : TComponent; PrimaryChartAxis : APgChartAxis; MetaSeries : APgMetaSeries; XIndex : Integer) : APgSeries;
begin
   Result := nil;
   case PrimaryChartAxis.axis of
      atX:
         if Assigned(MetaSeries) then
           Result := LogRatiosSeries.Create(AOwner, PrimaryChartAxis, MetaSeries, XIndex)
         else
            Result := DsYearAxisSeries.Create(AOwner, PrimaryChartAxis, MetaSeries, XIndex);
   end; // case
end; // LogRatiosChart.CreateSeries


constructor LogRatiosChart.Create(AOwner : TComponent; datacontainer : TasacsDataContainer; _Ds : Integer; Age : Integer);
var
   M : APgMetaSeries;
   I : Integer;
   SeriesName : string;
begin
   m_Age := Age;
   inherited Create(AOwner, datacontainer, _DS);
   m_Age := Age;
   Caption := inttostr(datacontainer.FirstAge + Age) + ' - ' + inttostr(datacontainer.FirstAge + Age + 1);
   ShowSeriesLegend := true;
   ChartLegend := Caption;
   ShowChartLegend := true;
   ShowMeanLegend := false;
//      ChartAxis[atX].ShowIntervalSeries := true;
   PlotType := ptCategorySingle;
   chartaxis[atX].ContinuousGroupData := false;
   chartaxis[atX].ShowMinorTick := false;
   chartaxis[atX].ShowMajorGrid := true;
//   chartaxis[aty].DefaultMin := true;
   chartaxis[aty].ShowLabel := false;
   chartaxis[atX].ShowLabel := false;
   //chartaxis[atX].AxisLabelPosition := alpCenter;
//   chartaxis[aty].AxisLabel := 'Age';
   chartaxis[aty].AxisLabelPosition := alpCenter;
   chartaxis[atY].ShowMajorGrid := true;
   chartaxis[aty].DefaultAxisLabel := false;
   chartaxis[aty].ShowMinorTick := false;
   chartaxis[aty].DefaultMin := true;
//   chartaxis[aty].MinValue := 0;
   // Setup meta series at y axis:
   with ChartAxis[atY].MetaSeries do
   begin
      for I := 1 to 2 do
      begin
         case I of
            1: SeriesName := 'Log ratio';
            2: SeriesName := 'Compansated log ratio';
         end;
         M := TasacsMetaSeries.Create(ChartAxis[aty], nil, SeriesName, I);
         Add(Pointer(M));
         M.BarPlot := false;
         M.Scatter := true;
         M.LinePlot := true;
         M.SuperTrend := true;
         M.Trend.TrendFunctionType := ftMovAvg;
      end;
   end;
end; // LogRatiosChart.Create


///////////////////////////////
// LogRatiosSeries


function LogRatiosSeries.GetInternalValue(I : Integer; SeriesVariable : PgSeriesVariable; MetaSeries : ApgMetaSeries) : Double;
var
   S : Integer;
   Compansate : Boolean;
   iYear : Integer;
   obsdata, compdata : FloatMatrix;
   Obs0, Obs1, ObsVal, CompVal, Comp0, Comp1 : double;
begin
   case SeriesVariable of
      svIntervalDelta:
         with LogRatiosChart(PrimaryChartAxis.ChartComp) do
         begin
            S := Self.MetaSeries.ChartAxis.MetaSeries.IndexOf(Pointer(Self.MetaSeries));
            Compansate := S = 1;
            with DataSourceContainer(DataContainer.DS[DS]) do
            begin
               obsdata := FloatMatrix(DsData[ord(dsObs)]);
               compdata := FloatMatrix(DsData[ord(dsCblFac)]);
            end;
            iYear := I + MinCategory;
            Obs0 := obsdata[iYear, Age];
            Obs1 := obsdata[iYear + 1, Age + 1];
            ObsVal := 0;
            CompVal := 0;
            if (Obs0 > 0) and (Obs1 > 0) then
               ObsVal := ln(Obs0 / Obs1);
            if (ObsVal > 0) and Compansate then
            begin
               Comp0 := compdata[iYear, Age];
               Comp1 := compdata[iYear + 1, Age + 1];
               if (Comp0 > 0) and (Comp1 > 0) then
                  CompVal := ln(Comp0 / Comp1);
            end;
            Result := ObsVal - CompVal
         end;
   else
      Result := inherited GetInternalValue(I, SeriesVariable, MetaSeries)
   end; // case
end; // LogRatiosSeries.GetInternalValue


function LogRatiosSeries.GetShowZeros() : Boolean;
begin
   Result := false;
end;


//////////////////////////////////
// LogRatioYearAxis


function LogRatioYearAxis.GetNumDimensionElms : Integer;
begin
   Result := inherited GetNumDimensionElms() - 1
end; // LogRatioYearAxis.GetNumDimensionElms



end.
